<template>
  <div>
    <u-form
      :model="form"
      ref="uForm"
      :error-type="errorType"
      label-width="160rpx"
      label-align="center"
    >
      <u-form-item prop="commentorName" label="昵称"
        ><u-input v-model="form.commentorName"
      /></u-form-item>
      <u-form-item prop="comments" label="内容"
        ><u-input
          type="textarea"
          v-model="form.comments"
          height="200"
          maxlength="1000"
      /></u-form-item>
      <u-button type="success" @click="confim">发表评论</u-button>
    </u-form>
    <u-toast ref="uToast" />
  </div>
</template>
<script>
export default {
  data() {
    return {
      errorType: ["toast"],
      form: {
        commentorName: "",
        comments: "",
        parentId: 0,
        articleId: "",
        createTime: ""
      },
      rules: {
        commentorName: [
          {
            required: true,
            message: "昵称不能为空",
            trigger: "blur"
          }
        ],
        comments: [
          {
            required: true,
            message: "内容不能为空",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    CurentTime() {
      var now = new Date();
      var year = now.getFullYear(); //年
      var month = now.getMonth() + 1; //月
      var day = now.getDate(); //日
      var clock = year + "-";
      if (month < 10) clock += "0";
      clock += month + "-";
      if (day < 10) clock += "0";
      clock += day + " ";
      return clock;
    },
    confim() {
      this.$refs["uForm"].validate(valid => {
        if (valid) {
          this.form.createTime = this.CurentTime();
          let that = this;
          let token = this.$store.state.token
          uni.request({
            url: "http://39.106.224.223:8088/cms/ordinary/comment/post",
            header: {
              "content-type": "application/json",
              Authorization: token
            },
            method: "POST",
            data: that.form,
            success: res => {
              if (res.data.code == 200) {
                this.$refs.uToast.show({
                  title: "评论成功",
                  type: "success"
                });
                uni.reLaunch({
                  url: "./articleDetail?id=" + this.form.articleId
                });
              } else {
                this.$refs.uToast.show({
                  title: "评论失败",
                  type: "error"
                });
              }
            },
            fail(er) {
              console.log(er);
            }
          });
        }
      });
    }
  },
  onLoad(options) {
    let id = JSON.parse(options.id); // 字符串转对象
    this.form.articleId = id;
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  }
};
</script>
<style lang="">
</style>